package com.zcsy.commons.util;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

import com.zcsy.commons.constants.Constants;


public class MD5Util {

    public static final String USER_KEY = "zcsy!@#$%^&*ibms";

    /***
     * MD5加码 生成32位md5码
     */
    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        }
        catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        byte[] md5Bytes = null;
		try {
			md5Bytes = md5.digest(inStr.getBytes(Constants.DEFAULT_CHARSET));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++ ) {
            int val = ((int)md5Bytes[i]) & 0xff;
            if (val < 16) hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMD5(String inStr) {

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++ ) {
            a[i] = (char)(a[i] ^ 't');
        }
        String s = new String(a);
        return s;

    }
    
    private static String byteArrayToHexString(byte b[]) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
			resultSb.append(byteToHexString(b[i]));

		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	/**
	 * @Description MD5编码<br>
	 * 
	 * @param origin
	 * @param charsetname
	 * @return 
	 * @author tanghan barbadosliy@163.com
	 * @date 2015年11月9日
	 * @see 
	 */
	public static String MD5Encode(String origin, String charsetname) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if (charsetname == null || "".equals(charsetname))
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			else
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(charsetname)));
		} catch (Exception exception) {
		}
		return resultString;
	}

	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
	
	public static void main(String[] args) {
		String msg = "appid=wx18dee29515d4402b&attach=微信支付&body=邢粮梳篦-四大美女&mch_id=1269363901&nonce_str=sar3zo5a344zssj7&notify_url=http://m.cjfygift.com/memberCenter&openid=o1rY0wz51vilK_6k8ofEEzUtRjsQ&out_trade_no=ddcf201511090016&spbill_create_ip=223.104.20.102&total_fee=1&trade_type=JSAPI&key=lfshrefy4783ybrv3wunxwvr43ytv984";
		
		System.out.println(MD5Encode(msg, "UTF-8"));
	}
	
	
	/**
	 * 通过用户名密码生成密钥
	 */
	public static String generalPassword(final String userName, final String password) {
		String decryptStr="";
		try {
			decryptStr = DESEncryption.decrypt(password, userName);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(userName.indexOf("@") != -1){
			String shortUserName = userName.substring(0, userName.indexOf("@"));
			return MD5Util.string2MD5(shortUserName.toLowerCase() + MD5Util.USER_KEY + MD5Util.string2MD5(decryptStr));
		}else{
			return MD5Util.string2MD5(userName.toLowerCase() + MD5Util.USER_KEY + MD5Util.string2MD5(decryptStr));
		}
		
	}

}
